// V-backend/src/models/OrderItem.js
module.exports = (sequelize, DataTypes) => {
  const OrderItem = sequelize.define('OrderItem', {
    order_item_id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
      field: 'order_item_id'
    },
    order_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      field: 'order_id'
    },
    product_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      field: 'product_id'
    },
    product_name: {
      type: DataTypes.STRING(255),
      allowNull: false,
      field: 'product_name'
    },
    product_price: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: false,
      field: 'product_price'
    },
    quantity: {
      type: DataTypes.INTEGER,
      allowNull: false,
      field: 'quantity'
    },
    subtotal: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: false,
      field: 'subtotal'
    },
    created_at: {
      type: DataTypes.DATE,
      defaultValue: DataTypes.NOW,
      field: 'created_at'
    }
  }, {
    tableName: 'v_order_item',
    timestamps: false,
    freezeTableName: true
  });

  OrderItem.associate = function(models) {
    OrderItem.belongsTo(models.Order, {
      foreignKey: 'order_id',
      as: 'order'
    });
    
    OrderItem.belongsTo(models.Product, {
      foreignKey: 'product_id',
      as: 'product'
    });
  };

  return OrderItem;
};